<?php

/**
 * ajax返回数据
 * @param int $status       状态码
 * @param string $msg       描述
 * @param array $data       数据
 * @return string
 */
if (!method_exists('ajax_return')) {
    function ajax_return($status = 1, $msg = '', $data = [])
    {
        $res['status'] = $status;
        $res['msg']    = $msg;
        $res['data']   = $data;
        exit(json_encode($res));
    }
}

/**
 * 时间日期转时间戳格式，精确到毫秒
 */
function get_data_format($time)
{
    list($usec, $sec) = explode(".", $time);
    $date             = strtotime($usec);
    $return_data      = str_pad($date . $sec, 13, "0", STR_PAD_RIGHT); //不足13位。右边补0
    return $return_data;
}

//去掉数字逗号
function str_d($str)
{
    $arr    = explode(',', $str);
    $newstr = '';
    foreach ($arr as $k => $v) {
        $newstr .= $v;
    }

    return $newstr;
}

//获取当前活动日期
//$type =1 日期 其他时间戳
function get_activity_date($type = 1)
{
    $r = M('seeting')->where(['name' => 'ACTIVITY_START_DATE'])->find();
    if ($type == 1) {
        return $r['value'];
    } else {
        return strtotime($r['value']);
    }
}

//短信随机码
function sms_rand_key()
{
    $pool     = '0123456789';
    $rand_key = '';
    for ($i = 0; $i < 6; $i++) {
        $rand_key .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
    }

    return $rand_key;
}

//发邮件生成的编码
function getno($id)
{
    $no = 'hn' . date('Ymd') . $id;
    return $no;
}

//返回跟目录地址url
function geturl()
{
    //返回服务器真实路径
    $Absolute_Path = $_SERVER['SCRIPT_FILENAME'];
    //截取index.php
    $Absolute_Path = substr($Absolute_Path, 0, -10);
    return $Absolute_Path;
}

if (!method_exists('random_str')) {
    function random_str($length)
    {
        //生成一个包含 大写英文字母, 小写英文字母, 数字 的数组
        $arr     = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
        $str     = '';
        $arr_len = count($arr);
        for ($i = 0; $i < $length; $i++) {
            $rand = mt_rand(0, $arr_len - 1);
            $str .= $arr[$rand];
        }
        return $str;
    }
}

//获取跟进人/抄送人信息
function get_name_info($str){
    $arr = explode(',', $str);
    foreach ($arr as $k => $v) {
        if($v){
            $nickname = M('user')->where(["id"=>$v])->getField('nickname');
            $res_str .= $nickname.'&nbsp;';

        }       
        
    }
    return $res_str;
}


